﻿using System;
using System.Data;
using System.Text;
using MySql.Data.MySqlClient;
using Maticsoft.DBUtility;//Please add references
namespace hcrainMvc.DAL
{
	/// <summary>
	/// 数据访问类:herp_inoutorderlist
	/// </summary>
	public partial class herp_inoutorderlist
	{
		public herp_inoutorderlist()
		{}
		#region  BasicMethod

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(string gid,long id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from herp_inoutorderlist");
			strSql.Append(" where gid=@gid and id=@id ");
			MySqlParameter[] parameters = {
					new MySqlParameter("@gid", MySqlDbType.VarChar,36),
					new MySqlParameter("@id", MySqlDbType.Int64,20)			};
			parameters[0].Value = gid;
			parameters[1].Value = id;

			return DbHelperMySQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(hcrainMvc.Model.herp_inoutorderlist model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into herp_inoutorderlist(");
			strSql.Append("gid,ordergid,spid,spname,count,price,unit,state,desc,storeid,spmaintype,spsubtype,dataadduserid,datadepid,databranchid,datadepname,databranchname,dataaddusername,dataaddtime)");
			strSql.Append(" values (");
			strSql.Append("@gid,@ordergid,@spid,@spname,@count,@price,@unit,@state,@desc,@storeid,@spmaintype,@spsubtype,@dataadduserid,@datadepid,@databranchid,@datadepname,@databranchname,@dataaddusername,@dataaddtime)");
			MySqlParameter[] parameters = {
					new MySqlParameter("@gid", MySqlDbType.VarChar,36),
					new MySqlParameter("@ordergid", MySqlDbType.VarChar,36),
					new MySqlParameter("@spid", MySqlDbType.Int64,20),
					new MySqlParameter("@spname", MySqlDbType.VarChar,100),
					new MySqlParameter("@count", MySqlDbType.Decimal,10),
					new MySqlParameter("@price", MySqlDbType.Decimal,10),
					new MySqlParameter("@unit", MySqlDbType.VarChar,40),
					new MySqlParameter("@state", MySqlDbType.VarChar,100),
					new MySqlParameter("@desc", MySqlDbType.VarChar,200),
					new MySqlParameter("@storeid", MySqlDbType.Int64,20),
					new MySqlParameter("@spmaintype", MySqlDbType.VarChar,80),
					new MySqlParameter("@spsubtype", MySqlDbType.VarChar,80),
					new MySqlParameter("@dataadduserid", MySqlDbType.Int32,11),
					new MySqlParameter("@datadepid", MySqlDbType.Int32,11),
					new MySqlParameter("@databranchid", MySqlDbType.Int32,11),
					new MySqlParameter("@datadepname", MySqlDbType.VarChar,100),
					new MySqlParameter("@databranchname", MySqlDbType.VarChar,100),
					new MySqlParameter("@dataaddusername", MySqlDbType.VarChar,100),
					new MySqlParameter("@dataaddtime", MySqlDbType.DateTime)};
			parameters[0].Value = model.gid;
			parameters[1].Value = model.ordergid;
			parameters[2].Value = model.spid;
			parameters[3].Value = model.spname;
			parameters[4].Value = model.count;
			parameters[5].Value = model.price;
			parameters[6].Value = model.unit;
			parameters[7].Value = model.state;
			parameters[8].Value = model.desc;
			parameters[9].Value = model.storeid;
			parameters[10].Value = model.spmaintype;
			parameters[11].Value = model.spsubtype;
			parameters[12].Value = model.dataadduserid;
			parameters[13].Value = model.datadepid;
			parameters[14].Value = model.databranchid;
			parameters[15].Value = model.datadepname;
			parameters[16].Value = model.databranchname;
			parameters[17].Value = model.dataaddusername;
			parameters[18].Value = model.dataaddtime;

			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(hcrainMvc.Model.herp_inoutorderlist model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update herp_inoutorderlist set ");
			strSql.Append("ordergid=@ordergid,");
			strSql.Append("spid=@spid,");
			strSql.Append("spname=@spname,");
			strSql.Append("count=@count,");
			strSql.Append("price=@price,");
			strSql.Append("unit=@unit,");
			strSql.Append("state=@state,");
			strSql.Append("desc=@desc,");
			strSql.Append("storeid=@storeid,");
			strSql.Append("spmaintype=@spmaintype,");
			strSql.Append("spsubtype=@spsubtype,");
			strSql.Append("dataadduserid=@dataadduserid,");
			strSql.Append("datadepid=@datadepid,");
			strSql.Append("databranchid=@databranchid,");
			strSql.Append("datadepname=@datadepname,");
			strSql.Append("databranchname=@databranchname,");
			strSql.Append("dataaddusername=@dataaddusername,");
			strSql.Append("dataaddtime=@dataaddtime");
			strSql.Append(" where id=@id");
			MySqlParameter[] parameters = {
					new MySqlParameter("@ordergid", MySqlDbType.VarChar,36),
					new MySqlParameter("@spid", MySqlDbType.Int64,20),
					new MySqlParameter("@spname", MySqlDbType.VarChar,100),
					new MySqlParameter("@count", MySqlDbType.Decimal,10),
					new MySqlParameter("@price", MySqlDbType.Decimal,10),
					new MySqlParameter("@unit", MySqlDbType.VarChar,40),
					new MySqlParameter("@state", MySqlDbType.VarChar,100),
					new MySqlParameter("@desc", MySqlDbType.VarChar,200),
					new MySqlParameter("@storeid", MySqlDbType.Int64,20),
					new MySqlParameter("@spmaintype", MySqlDbType.VarChar,80),
					new MySqlParameter("@spsubtype", MySqlDbType.VarChar,80),
					new MySqlParameter("@dataadduserid", MySqlDbType.Int32,11),
					new MySqlParameter("@datadepid", MySqlDbType.Int32,11),
					new MySqlParameter("@databranchid", MySqlDbType.Int32,11),
					new MySqlParameter("@datadepname", MySqlDbType.VarChar,100),
					new MySqlParameter("@databranchname", MySqlDbType.VarChar,100),
					new MySqlParameter("@dataaddusername", MySqlDbType.VarChar,100),
					new MySqlParameter("@dataaddtime", MySqlDbType.DateTime),
					new MySqlParameter("@id", MySqlDbType.Int64,20),
					new MySqlParameter("@gid", MySqlDbType.VarChar,36)};
			parameters[0].Value = model.ordergid;
			parameters[1].Value = model.spid;
			parameters[2].Value = model.spname;
			parameters[3].Value = model.count;
			parameters[4].Value = model.price;
			parameters[5].Value = model.unit;
			parameters[6].Value = model.state;
			parameters[7].Value = model.desc;
			parameters[8].Value = model.storeid;
			parameters[9].Value = model.spmaintype;
			parameters[10].Value = model.spsubtype;
			parameters[11].Value = model.dataadduserid;
			parameters[12].Value = model.datadepid;
			parameters[13].Value = model.databranchid;
			parameters[14].Value = model.datadepname;
			parameters[15].Value = model.databranchname;
			parameters[16].Value = model.dataaddusername;
			parameters[17].Value = model.dataaddtime;
			parameters[18].Value = model.id;
			parameters[19].Value = model.gid;

			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(long id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from herp_inoutorderlist ");
			strSql.Append(" where id=@id");
			MySqlParameter[] parameters = {
					new MySqlParameter("@id", MySqlDbType.Int64)
			};
			parameters[0].Value = id;

			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(string gid,long id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from herp_inoutorderlist ");
			strSql.Append(" where gid=@gid and id=@id ");
			MySqlParameter[] parameters = {
					new MySqlParameter("@gid", MySqlDbType.VarChar,36),
					new MySqlParameter("@id", MySqlDbType.Int64,20)			};
			parameters[0].Value = gid;
			parameters[1].Value = id;

			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from herp_inoutorderlist ");
			strSql.Append(" where id in ("+idlist + ")  ");
			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public hcrainMvc.Model.herp_inoutorderlist GetModel(long id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id,gid,ordergid,spid,spname,count,price,unit,state,desc,storeid,spmaintype,spsubtype,dataadduserid,datadepid,databranchid,datadepname,databranchname,dataaddusername,dataaddtime from herp_inoutorderlist ");
			strSql.Append(" where id=@id");
			MySqlParameter[] parameters = {
					new MySqlParameter("@id", MySqlDbType.Int64)
			};
			parameters[0].Value = id;

			hcrainMvc.Model.herp_inoutorderlist model=new hcrainMvc.Model.herp_inoutorderlist();
			DataSet ds=DbHelperMySQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public hcrainMvc.Model.herp_inoutorderlist DataRowToModel(DataRow row)
		{
			hcrainMvc.Model.herp_inoutorderlist model=new hcrainMvc.Model.herp_inoutorderlist();
			if (row != null)
			{
				if(row["id"]!=null && row["id"].ToString()!="")
				{
					model.id=long.Parse(row["id"].ToString());
				}
				if(row["gid"]!=null)
				{
					model.gid=row["gid"].ToString();
				}
				if(row["ordergid"]!=null)
				{
					model.ordergid=row["ordergid"].ToString();
				}
				if(row["spid"]!=null && row["spid"].ToString()!="")
				{
					model.spid=long.Parse(row["spid"].ToString());
				}
				if(row["spname"]!=null)
				{
					model.spname=row["spname"].ToString();
				}
				if(row["count"]!=null && row["count"].ToString()!="")
				{
					model.count=decimal.Parse(row["count"].ToString());
				}
				if(row["price"]!=null && row["price"].ToString()!="")
				{
					model.price=decimal.Parse(row["price"].ToString());
				}
				if(row["unit"]!=null)
				{
					model.unit=row["unit"].ToString();
				}
				if(row["state"]!=null)
				{
					model.state=row["state"].ToString();
				}
				if(row["desc"]!=null)
				{
					model.desc=row["desc"].ToString();
				}
				if(row["storeid"]!=null && row["storeid"].ToString()!="")
				{
					model.storeid=long.Parse(row["storeid"].ToString());
				}
				if(row["spmaintype"]!=null)
				{
					model.spmaintype=row["spmaintype"].ToString();
				}
				if(row["spsubtype"]!=null)
				{
					model.spsubtype=row["spsubtype"].ToString();
				}
				if(row["dataadduserid"]!=null && row["dataadduserid"].ToString()!="")
				{
					model.dataadduserid=int.Parse(row["dataadduserid"].ToString());
				}
				if(row["datadepid"]!=null && row["datadepid"].ToString()!="")
				{
					model.datadepid=int.Parse(row["datadepid"].ToString());
				}
				if(row["databranchid"]!=null && row["databranchid"].ToString()!="")
				{
					model.databranchid=int.Parse(row["databranchid"].ToString());
				}
				if(row["datadepname"]!=null)
				{
					model.datadepname=row["datadepname"].ToString();
				}
				if(row["databranchname"]!=null)
				{
					model.databranchname=row["databranchname"].ToString();
				}
				if(row["dataaddusername"]!=null)
				{
					model.dataaddusername=row["dataaddusername"].ToString();
				}
				if(row["dataaddtime"]!=null && row["dataaddtime"].ToString()!="")
				{
					model.dataaddtime=DateTime.Parse(row["dataaddtime"].ToString());
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id,gid,ordergid,spid,spname,count,price,unit,state,desc,storeid,spmaintype,spsubtype,dataadduserid,datadepid,databranchid,datadepname,databranchname,dataaddusername,dataaddtime ");
			strSql.Append(" FROM herp_inoutorderlist ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperMySQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM herp_inoutorderlist ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.id desc");
			}
			strSql.Append(")AS Row, T.*  from herp_inoutorderlist T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperMySQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			MySqlParameter[] parameters = {
					new MySqlParameter("@tblName", MySqlDbType.VarChar, 255),
					new MySqlParameter("@fldName", MySqlDbType.VarChar, 255),
					new MySqlParameter("@PageSize", MySqlDbType.Int32),
					new MySqlParameter("@PageIndex", MySqlDbType.Int32),
					new MySqlParameter("@IsReCount", MySqlDbType.Bit),
					new MySqlParameter("@OrderType", MySqlDbType.Bit),
					new MySqlParameter("@strWhere", MySqlDbType.VarChar,1000),
					};
			parameters[0].Value = "herp_inoutorderlist";
			parameters[1].Value = "id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperMySQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

